﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;

namespace WebApp.Product
{
    public partial class ProductSupCategory : System.Web.UI.Page
    {
        private CategoryManager m_CategoryManager = new CategoryManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSupCategory();
                BindLVSupCategory();
            }
        }
        private void BindSupCategory()
        {
            this.rptSupCategory.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L)).OrderBy(theCategory => theCategory.Index);
            this.rptSupCategory.DataBind();
        }

        private void BindLVSupCategory()
        {
            this.ListView1.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetTopCategryByName(CategoryManager.ETopCategory.Category_L)).OrderBy(theCategory => theCategory.Index);
            this.ListView1.DataBind();
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            Repeater rpt = e.Item.FindControl("RptCategory") as Repeater;

            rpt.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value))).OrderBy(theCategory => theCategory.Index);
            rpt.DataBind();
        }

        protected void RptCategory_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            Repeater rpt = e.Item.FindControl("RptSubCategory") as Repeater;

            rpt.DataSource = this.m_CategoryManager.GetSubCateogoriesBySupCategory(this.m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value))).OrderBy(theCategory => theCategory.Index);
            rpt.DataBind();


            //HiddenField hfCategoryId = e.Item.FindControl("hfCagegoryId") as HiddenField;
            //HyperLink hlcategory = e.Item.FindControl("HLSubCategoryId") as HyperLink;

            //string upCategoryId = m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value)).SupCategoryId.ToString();

            //hlcategory.NavigateUrl = "~/Product/ProductCategory.aspx?CID=" + upCategoryId + "&SID=" + hfCategoryId.Value;
        }

        protected void RptSubCategory_ItemDataBound(object source, RepeaterItemEventArgs e)
        {
            HiddenField hfCategoryId = e.Item.FindControl("hfSubCategoryId") as HiddenField;
            HyperLink hlcategory = e.Item.FindControl("HLSubCategoryId") as HyperLink;
            if (hfCategoryId != null && hlcategory != null)
            {
                string upCategoryId = m_CategoryManager.GetCategoryById(new Guid(hfCategoryId.Value)).SupCategoryId.ToString();

                hlcategory.NavigateUrl = "/Product/ProductCategory.aspx?CID=" + upCategoryId + "&SID=" + hfCategoryId.Value;
            }
        }



    }
}